<template>
  <!-- 角色管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="角色名称:" prop="roleName">
            <el-input
              v-model.trim="filterForm.roleName"
              class="filter-item limit"
              maxlength="30"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
          <el-form-item label="角色编码:" prop="roleCode">
            <el-input
              v-model.trim="filterForm.roleCode"
              class="filter-item limit"
              maxlength="30"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" class="filter-item" @click="handleFilter"> 搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['BUTTON_ROLE_ADD']"
            class="filter-item"
            icon="el-icon-plus"
            type="primary"
            @click="addData"
          >
            新增
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="listLoading"
          border
          highlight-current-row
          :row-key="getRowKey"
          :data="list"
          style="width: 100%;text-align:center"
          :header-cell-style="headerCellStyle"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="table_index"
          />
          <el-table-column
            v-if="columns[0].show"
            prop="roleName"
            label="角色名称"
            sortable
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columns[1].show"
            prop="roleCode"
            label="角色编码"
            sortable
            show-overflow-tooltip
          />

          <!-- <el-table-column
            v-if="columns[3].show"
            prop="roleSystemFlag"
            label="角色级别"
            sortable
          >
            <template slot-scope="scope">
              <el-tag type="danger">
                {{ scope.row.roleSystemFlag == 'Y' ? '系统角色': '普通角色' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[4].show"
            prop="roleTypeCode"
            label="角色类型"
            sortable
          >
            <template slot-scope="scope">
              <el-tag type="danger">
                {{ scope.row.roleTypeCode == 'role_system' ? '系统': scope.row.roleTypeCode == 'role_c' ? 'C端':scope.row.roleTypeCode == 'role_b' ? 'B端':'未知' }}
              </el-tag>
            </template>
          </el-table-column> -->
          <el-table-column
            v-if="columns[2].show"
            prop="dataScopeType"
            label="数据范围"
            sortable
          >
            <template slot-scope="scope">
              <el-tag style="border:none" :type="scope.row.dataScopeType | statusFilter">
                {{ scope.row.dataScopeType == 10 ? '仅本人数据': scope.row.dataScopeType == 20 ? '本部门数据':scope.row.dataScopeType == 30 ? '本部门及以下数据':scope.row.dataScopeType == 40 ? '指定部门数据':scope.row.dataScopeType == 50 ? '全部数据':'未知' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[3].show"
            prop="roleSort"
            label="排序"
            sortable
          />
          <!-- <el-table-column
            v-if="columns[6].show"
            prop="statusFlag"
            label="状态"
            sortable
          >
            <template slot-scope="scope">
              <el-tag :type="scope.row.statusFlag | statusFilter">
                {{ scope.row.statusFlag == 1 ? '启用': scope.row.statusFlag == 2 ? '禁用':'未知' }}
              </el-tag>
            </template>
          </el-table-column> -->
          <el-table-column
            v-if="columns[4].show"
            prop="remark"
            label="备注"
            sortable
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            :fixed="$store.getters.device == 'mobile'? null :'right' "
            label="操作"
            width="230"
          >
            <template slot-scope="scope">
              <el-button
                v-permission="['BUTTON_ROLE_EDIT']"
                size="mini"
                @click="handleEdit(scope)"
              >修改</el-button>
              <el-button
                v-permission="['BUTTON_ROLE_DELETE']"
                size="mini"
                type="danger"
                @click="handleDelete(scope)"
              >删除</el-button>
              <el-dropdown style="padding-left: 10px">
                <el-button v-permission="['BUTTON_ROLE_MORE']" size="mini">
                  更多<i class="el-icon-arrow-down el-icon--right" />
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="editDataScope(scope.row.roleId)">设置数据范围</el-dropdown-item>
                  <el-dropdown-item @click.native="editAssignMenu(scope.row.roleId)">分配菜单按钮</el-dropdown-item>
                  <!-- <el-dropdown-item @click.native="editAssignApi(scope.row.roleId)">分配接口</el-dropdown-item> -->
                  <el-dropdown-item @click.native="openApi(scope.row)">分配接口</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="filterForm.pageNo"
          :limit.sync="filterForm.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <edit :title="edit.title" :visible="edit.visible" :form-data="edit.formData" :close-form="closeForm" />
    <data-scope ref="refDataScope" :visible="dataScope.visible" :form-data="dataScope.formData" :close-form="closeForm" />
    <assign-menu ref="refAssignMenu" :visible="assignMenu.visible" :form-data="assignMenu.formData" :close-form="closeForm" />
    <!-- <assign-api ref="refAssignApi" :visible="assignApi.visible" :form-data="assignApi.formData" :close-form="closeForm" /> -->
    <ApiRef ref="ApiRef" />
  </div>
</template>
<script>
import roleApi from '@/framework/api/role'
import Edit from './roleEdit'
import DataScope from './roleDataScope'
import AssignMenu from './roleAssignMenu'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'
import ApiRef from './compontents/roleAssignApi'
export default {
  name: 'AuthRole',
  filters: {
    statusFilter(status) {
      const filter = { 0: 'danger', 1: 'success', 50: 'danger' }
      return filter[status]
    }
  },

  components: {
    Edit,
    DataScope,
    AssignMenu,
    ApiRef
  },
  mixins: [dialogColumn],
  data() {
    return {
      listLoading: false,
      list: [],
      filterForm: {
        roleName: '',
        roleCode: '',
        pageNo: 1,
        pageSize: 10
      },
      total: 0,
      // 列信息
      columns: [
        { prop: 'roleName', label: `角色名称`, show: true },
        { prop: 'roleCode', label: `角色编码`, show: true },
        { prop: 'dataScopeType', label: `数据范围类型`, show: true },
        { prop: 'roleSort', label: `排序`, show: true },
        { prop: 'remark', label: `备注`, show: true }
      ],
      edit: {
        title: '',
        visible: false,
        formData: {}
      },
      dataScope: {
        visible: false,
        formData: {}
      },
      assignMenu: {
        visible: false,
        formData: {}
      },
      assignApi: {
        visible: false,
        formData: {}
      },
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      roleListLength: 0,

      // 排序
      sort: '',
      order: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 列表序号
    table_index(index) {
      return (this.filterForm.pageNo - 1) * this.filterForm.pageSize + index + 1
    },
    // 全部数据排序
    customSorting(parameter) {
      this.sort = parameter.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      if (parameter.order.startsWith('asc')) {
        this.order = 'asc'
      } else {
        this.order = 'desc'
      }
      this.getList()
    },
    // 列表获取数据
    getList() {
      this.listLoading = true
      roleApi.fetchList(this.filterForm).then((res) => {
        this.list = res.data.rows
        this.total = res.data.totalRows
        this.roleListLength = res.data.rows.length
        this.listLoading = false
      })
    },
    // 搜索
    handleFilter() {
      this.filterForm.pageNo = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.filterFormRef.resetFields('filterForm')
      this.handleFilter()
    },
    // 编辑按钮
    async handleEdit(scope) {
      if (scope.row.roleCode === 'superAdmin') {
        this.$dtMessage({
          type: 'error',
          message: '无法修改超级管理员'
        })
        return
      }
      const res = await roleApi.getRoleById(scope.row.roleId)
      this.edit.formData = res.data
      this.edit.title = '修改角色'
      this.edit.visible = true
    },
    // 删除按钮
    handleDelete(scope) {
      if (scope.row.roleCode === 'superAdmin') {
        this.$dtMessage({
          type: 'error',
          message: '无法删除超级管理员'
        })
        return
      }
      this.$dtMessageBox({ title: '提示', message: '确定要删除“' + scope.row.roleName + '”吗?', type: 'warning' }).then(() => {
        roleApi.deleteRole(scope.row.roleId).then((res) => {
          this.$dtMessage({
            type: 'success',
            message: '删除成功'
          })
          this.getList()
        })
      }).catch(() => {
        // 取消不做操作
      })
    },
    // 添加按钮
    addData() {
      this.edit.title = '新增角色'
      this.edit.visible = true
    },
    // 刷新列表 用于监听子组件新增编辑后触发
    closeForm() {
      this.edit.formData = {}
      this.edit.visible = false
      this.dataScope.visible = false
      this.dataScope.formData = {}
      this.assignMenu.visible = false
      this.assignMenu.formData = {}
      this.assignApi.visible = false
      this.assignApi.formData = {}
      this.getList()
    },
    async editDataScope(id) {
      const res = await roleApi.getRoleById(id)
      // 改为懒加载
      const resBos = await roleApi.roleBindOrgScopeLazyAntdv({ roleId: res.data.roleId, orgId: 0 })
      var data = {
        'roleName': res.data.roleName,
        'dataScopeType': res.data.dataScopeType,
        'roleId': res.data.roleId,
        'tree': resBos.data
      }
      this.dataScope.formData = data
      this.dataScope.visible = true
      this.$refs.refDataScope.getRoleBindOrgScope(resBos.data, res.data)
    },
    async editAssignMenu(id) {
      const res = await roleApi.getRoleById(id)
      this.assignMenu.formData = res.data
      this.assignMenu.visible = true
      this.$refs.refAssignMenu.getMenuAndButton(id)
    },
    openApi(row) {
      this.$refs.ApiRef.openApi(row)
    },
    getRowKey(row) {
      return row.roleId
    }

  }
}

</script>

<style scoped>

</style>
